package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultConstant;
import com.yunzhi.domain.ResultDTO;
import com.yunzhi.util.RequestUtil;
import com.yunzhi.util.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.domain.Share;
import com.edais.service.SystemSettingService;
import com.edais.util.Consts;
import com.edais.util.Message;
import com.edais.util.StringUtil;
import com.lb.sssq.util.CommonUtil;

/**
 * 分享文案接口
 */
@Controller
@RequestMapping("/share")

public class ShareAction implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final Log logger = LogFactory.getLog(ShareAction.class);

    @Autowired
    private SystemSettingService systemSettingService;

    /**
     * @param @param request
     * @param @param response
     * @return void
     * @throws
     * @Description: 分享文案
     * @author chenshufeng
     * @date 2016-8-10
     */
    @RequestMapping(value = "/getShareSub")
    public void getShareSub(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> resultMap = new HashMap<String, Object>();

        try {

            if (!RequestUtil.validSign(request)) {
                logger.info("无效签名");
                ResponseUtil.write(response, ResultConstant.INVALID_SIGN);
                return;
            }

            JSONObject params = RequestUtil.decodeDataJson(request);
            if(null == params){
                logger.info("参数列表为空");
                ResponseUtil.write(response, new ResultDTO(ResultCode.FAILED,"参数列表为空"));
                return;
            }

            String from = params.getString("from");
            if (!"LBIOS".equals(from) && !"LBAndroid".equals(from) && !"LBWX".equals(from)) {
                logger.error("分享文案列表数据检查失败： from为空");
                ResponseUtil.write(response, new ResultDTO(ResultCode.FAILED, "from为空"));
                return;
            }


            // 查询投资理财列表
            Share share = systemSettingService.getSystemSettingByName();
            if (null != share) {
                String title = share.getShareTitle();
                String desc = share.getShareDesc();
                String link = share.getShareLink();
                String imgUrl = share.getShareImgUrl();
                if (StringUtil.isEmpty(title)) {
                    title = "我已加入联币大家庭，邀请你来！";
                }
                if (StringUtil.isEmpty(desc)) {
                    desc = "活期理财超8%25，当日计息，资金随存随取，快速提现无费用";
                }
                if (StringUtil.isEmpty(link)) {
                    link = "http://www.lianbijr.com/wxactivity/activity_20151103";
                }
                if (StringUtil.isEmpty(imgUrl)) {
                    imgUrl = "http://www.lianbijr.com/pic/weixin/activity/activity_20151008/logo.png";
                }
                resultMap.put("title", title);
                resultMap.put("desc", desc);
                resultMap.put("link", link);
                resultMap.put("imgUrl", imgUrl);

                ResultDTO<Map<String, Object>> result = new ResultDTO<>(ResultCode.SUCCESS);
                result.setRecord(resultMap);
                ResponseUtil.write(response, result);
            } else {
                resultMap.put("title", "我已加入联币大家庭，邀请你来！");
                resultMap.put("desc", "活期理财超8%，当日计息，资金随存随取，快速提现无费用");
                resultMap.put("link", "http://www.lianbijr.com/wxactivity/activity_20151103");
                resultMap.put("imgUrl", "http://www.lianbijr.com/pic/weixin/activity/activity_20151008/logo.png");
                ResultDTO<Map<String, Object>> result = new ResultDTO<>(ResultCode.SUCCESS);
                result.setRecord(resultMap);
                ResponseUtil.write(response, result);
            }
        } catch (Exception e) {
            logger.error("share/getShareSub分享文案接口异常:"
                    + com.edais.util.CommonUtil.printStackTraceToString(e));
            resultMap.put("title", "我已加入联币大家庭，邀请你来！");
            resultMap.put("desc", "活期理财超8%，当日计息，资金随存随取，快速提现无费用");
            resultMap.put("link", "http://www.lianbijr.com/wxactivity/activity_20151103");
            resultMap.put("imgUrl", "http://www.lianbijr.com/pic/weixin/activity/activity_20151008/logo.png");
            ResultDTO<Map<String, Object>> result = new ResultDTO<>(ResultCode.SUCCESS);
            result.setRecord(resultMap);
            ResponseUtil.write(response, result);
        }
    }
}
